package com.littleken.foodieService.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.littleken.foodieCommon.Response.ErrorCode;
import com.littleken.foodieCommon.Response.ServerResponse;
import com.littleken.foodieMapper.UserAddressMapper;
import com.littleken.foodiePojo.domain.UserAddressDO;
import com.littleken.foodiePojo.enums.UserAddressDefaultEnum;
import com.littleken.foodieService.AddressService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

/*
 * Create by littleken on 2020/11/25 10:15
 */
@Service
public class AddressServiceImpl implements AddressService {

    @Resource
    private UserAddressMapper userAddressMapper;

    @Override
    public List<UserAddressDO> getUserAllAddress(String userId) {
        List<UserAddressDO> userAddressDOS = Lists.newArrayList();
        QueryWrapper<UserAddressDO> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        userAddressDOS = userAddressMapper.selectList(wrapper);
        return userAddressDOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String saveOrUpdateUserAddress(UserAddressDO userAddressDO) {
        String addreddId = null;
        if (!StringUtils.isBlank(userAddressDO.getId())) {
            QueryWrapper<UserAddressDO> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id", userAddressDO.getUserId());
            Integer userAddressCount = userAddressMapper.selectCount(wrapper);
            if (userAddressCount == 0) {
                //第一个地址就设置为默认
                userAddressDO.setIsDefault(UserAddressDefaultEnum.IS_DEFAULT.getCode());
            }
            userAddressDO.setCreatedTime(LocalDate.now());
            userAddressDO.setUpdatedTime(LocalDate.now());
            userAddressMapper.insert(userAddressDO);
            addreddId = userAddressDO.getId();
        } else {
            userAddressDO.setUpdatedTime(LocalDate.now());
            userAddressMapper.updateById(userAddressDO);
            addreddId = userAddressDO.getId();
        }
        return addreddId;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ServerResponse deleteUserAddress(String addressId) {
        UserAddressDO userAddressDO = userAddressMapper.selectById(addressId);
        if (userAddressDO == null) {
            return ServerResponse.fail(ErrorCode.RECORD_NOT_EXIST);
        }
        int i = userAddressMapper.deleteById(addressId);
        return i == 1 ? ServerResponse.success(addressId) : ServerResponse.fail(ErrorCode.SQL_EXECUTE_ERR);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ServerResponse setAddressDefault(String userId, String addressId) {
        UserAddressDO userAddressDO = userAddressMapper.selectById(addressId);
        //记录判存在
        if (userAddressDO == null) {
            return ServerResponse.fail(ErrorCode.RECORD_NOT_EXIST);
        }
        //记录判是否已经是默认
        if (userAddressDO.getIsDefault().equals(UserAddressDefaultEnum.IS_DEFAULT.getCode())) {
            return ServerResponse.fail(ErrorCode.ADDRESS_IS_DEFAULT);
        }
        //改
        QueryWrapper<UserAddressDO> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<UserAddressDO> userAddressList = userAddressMapper.selectList(wrapper);
        for (UserAddressDO addressDO : userAddressList) {
            if (addressDO.getId().equals(addressId)){
                addressDO.setIsDefault(UserAddressDefaultEnum.IS_DEFAULT.getCode());
            }else {
                addressDO.setIsDefault(UserAddressDefaultEnum.NOT_DEFAULT.getCode());
            }
            addressDO.setUpdatedTime(LocalDate.now());
            userAddressMapper.updateById(addressDO);
        }
        return ServerResponse.success();
    }

    @Override
    public UserAddressDO getAddress(String addressId) {
        UserAddressDO userAddressDO = userAddressMapper.selectById(addressId);
        return userAddressDO;
    }
}
